from property import Property

class BooleanProperty(Property):
    ''' Used to represent True/False values.
    
        Indexed as ``int``.
    '''
    def __init__(self, **kwargs):
        kwargs['type'] = bool
        super(BooleanProperty, self).__init__(**kwargs)
        
    def encode_single(self, v):
        if not isinstance(v, bool):
            raise ValueError("BooleanProperty property %s value must be a bool. Got %s" % (self.get_id(), type(v)))
        return v
   
    def encode_repeated(self, v):
        for i, val in enumerate(v):
            if not isinstance(val, bool):
                raise ValueError("BooleanProperty property %s value must be a list of bool. Got %s at %s" % (self.get_id(), type(val), i))
        return v      

    def encode_other(self, other):
        if isinstance(other, (bool, int, long)):
            return 1 if other else 0
        else:
            raise ValueError("Filter value must be a bool, int or long")
            
    def encode_index_values(self, v):
        return [1 if v else 0]
   
    def get_index_types(self):
        return ['int']
        
    def get_index_keys(self):
        return ['%s_int' % self.get_id()]
        
    def decode(self, v):
        return True if v == 1 else False
